package cn.felord.core.controller.base;


import cn.felord.core.entity.SysUser;
import cn.felord.core.util.HttpUtil;
import cn.felord.core.util.ShiroUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.text.SimpleDateFormat;

/**
 * 基础控制器
 *
 * @author Gaojun.Zhou
 * @date 2016年12月27日 上午11:50:57
 */
public class SuperController {

    /**
     * The Logger.
     */
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * The Request.
     */
    @Autowired
    protected HttpServletRequest request;

    /**
     * The Response.
     */
    @Autowired
    protected HttpServletResponse response;

    /**
     * The Session.
     */
    @Autowired
    protected HttpSession session;

    /**
     * The Application.
     */
    @Autowired
    protected ServletContext application;

    /**
     * 是否为 post 请求
     *
     * @return the boolean
     */
    protected boolean isPost() {
        return HttpUtil.isPost(request);
    }


    /**
     * 是否为 get 请求
     *
     * @return the boolean
     */
    protected boolean isGet() {
        return HttpUtil.isGet(request);
    }


    /**
     * <p>
     * 获取分页对象
     * </p>
     *
     * @param <T>        the type parameter
     * @param pageNumber the page number
     * @return the page
     */
    protected <T> Page<T> getPage(int pageNumber) {
        return getPage(pageNumber, 15);
    }


    /**
     * Gets page.
     *
     * @param <T>        the type parameter
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @return page
     */
    protected <T> Page<T> getPage(int pageNumber, int pageSize) {
        return new Page<>(pageNumber, pageSize);
    }


    /**
     * 重定向至地址 url
     *
     * @param url 请求地址
     * @return string
     */
    protected String redirectTo(String url) {

        return "redirect:" + url;
    }

    /**
     * Gets current user.
     *
     * @return the current user
     */
    public SysUser getCurrentUser() {
        return ShiroUtil.getSessionUser();
    }

    /**
     * 返回 JSON 格式对象
     *
     * @param serializable 转换对象
     * @return string
     */
    protected String toJson(Serializable serializable) {
        return writeJson(serializable, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * To json string.
     *
     * @param serializable the serializable
     * @param format       the format
     * @return string
     */
    protected String toJson(Serializable serializable, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return writeJson(serializable, format);
    }

    private String writeJson(Serializable serializable, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(simpleDateFormat);
        try {
            return objectMapper.writeValueAsString(serializable);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
